/*******************************************************

	A simple link list implementation
	Course	: CSC3150
	Author	: Tsang Hing Chung
	Date	: 29 September, 2009
	Version	: 1.0

---------------------------------------------------------
    10/10/2009
        added an element into jobNode
            struct jobNode *ready_next
        added an element into jobNode
            pid_t child_pid

*********************************************************/
#include <sys/types.h>
#include <unistd.h>

#ifndef __LINKED_LIST_H__
#define __LINKED_LIST_H__

// maximun command length
#define MAX_CMD_SIZE 256
// maximum size of timeline array
#define MAX_TIMELINE_SIZE 1024
// maximum size of static string buffer
#define STRING_BUFFER_MAX 1024

// A simple job node of a linked list
struct jobNode{
	// Pointer points to the next node
	struct jobNode *next;
	// Pointer points to the next ready-queue node
	struct jobNode *ready_next;
	// Arrival time of the job
	int arrivalTime;
	// Duration of the job, -1 means infinite
	int duration;
	// Remaining execution time
	int timeRemain;
	// Remaining execution time
	int quantum;
	int runinfinity;
	float ptimeRemain;
	// Current index of timeline
	int timelineIndex;
	// timeline[i][0] store the i(th) start time
	// timeline[i][1] store the i(th) stop time
	float timeline[MAX_TIMELINE_SIZE][2];
	// The job's command(program name with arguments)
	char command[MAX_CMD_SIZE];
	// pid of the child - monitor
    pid_t child_pid;
};



/****************************************************************************
  function	: 	listLength
  parameters:	head - The first node of the list
  return	:	Lenght of the list
 ****************************************************************************/
int listLength(struct jobNode *head);



/****************************************************************************
  function	: 	createNode
  parameters:	arrivalTime - arrival time of the job
  				cmd			- program name and arguments
  				duration	- maximum time needed for a job, -1 means infinite
  return	:	A pointer points to the newly malloc()'d node
  				On error, return NULL
 ****************************************************************************/
struct jobNode *createNode(int arrivalTime, char *cmd, int duration);



/****************************************************************************
  function	: 	enqueue
  parameters:	ptrHeadNode - The address of the head node's pointer
  				newNode		- The new node to be enqueued
  return	:	void

  Note		: If newNode is NULL, nothing changed
  			  If *ptrHeadNode is NULL but newNode is not,
			  the new queue consist of the newNode only
 ****************************************************************************/
void enqueue(struct jobNode **ptrHeadNode, struct jobNode *newNode);



/****************************************************************************
  function	: 	dequeue
  parameters:	ptrHeadNode - The address of the head node's pointer
  return	:	The node pointed by ptrHeadNode
  				On error, return NULL

  Note		: If ptrHeadNode or *ptrHeadNode is NULL, nothing changed
  			  After a successfuly call, the head node will changed to
			  its next node, and possibly NULL
 ****************************************************************************/
struct jobNode *dequeue(struct jobNode **ptrHeadNode);



/****************************************************************************
  function	: 	removeNodeFromList
  parameters:	ptrHeadNode - The address of the head node's pointer
  				target		- The target to remove
  return	:	On success, return a pointer equal to target
  				If target is not found, return NULL

  Note		:	If target equals to *ptrHeadNode, the effect is the same as dequeue
 ****************************************************************************/
struct jobNode *removeNodeFromList(struct jobNode **ptrHeadNode, struct jobNode *target);



/******************************************************************************
  function	: 	freeList
  parameters:	ptrHeadNode - The address of the head node's pointer
  return	:	void

  Note		: After free()'ing every node, the head node will be set to NULL
  			  to prevent others to access it accidentally
 ******************************************************************************/
void freeList(struct jobNode **ptrHeadNode);



/******************************************************************************
  function	:   printList
  parameters:	headNode - A pointer points to the head node of the list
  return	:	void
 ******************************************************************************/
void printList(struct jobNode *headNode);


#endif
